package com.vne.skyemart.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.vne.skyemart.model.UserType;
import com.vne.skyemart.service.UserTypeService;

@Controller
@SessionAttributes
@RequestMapping("/userType")
public class UserTypeController {

	@Autowired
	private UserTypeService userTypeService;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView saveUserType(@ModelAttribute(" userType") UserType userType,
			BindingResult result) {
		userTypeService.addUserType(userType);
		return new ModelAndView("redirect:/userType.html");
	}

	@RequestMapping(method = RequestMethod.GET)
	public String listUserType(Model model) {
		model.addAttribute("userType",  userTypeService.listUserType());

		return "userTypeList";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addUserType(@ModelAttribute("userType") UserType userType,
			BindingResult result) {
		return new ModelAndView("addUserType");
	}

}